package pers.lance.jmsproject.client;


import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import javax.jms.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JmsClient {
    ConnectionFactory connectionFactory;
    Connection connection = null;
    Session session;
    String url = "tcp://10.222.29.196:61616";
    String queueName = "lance.queue";
    Map<String, MessageProducer> sendQueues = new ConcurrentHashMap<String, MessageProducer>();
    MessageProducer producer = null;
    public JmsClient() {
        ConnectionFactory factory = new ActiveMQConnectionFactory(url);
        Destination queue = new ActiveMQQueue(queueName);
        Connection con = null;
        try {
            con = factory.createConnection();
            con.start();
            session = con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }

    public void sendMessage(String data){
        try {
            producer.send(session.createTextMessage(data));
            System.out.println("send msg = " + data);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public void close(){
        try {
            producer.close();
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
